//加入路由转换为菜单数据的方法
const routerToMenuData = (m) => {
  return {
    routerName: m.name,
    label: m.meta.label,
    active: false,
    icon: m.meta.icon,
    role: m.meta.role,
    backdoor: m.meta.backdoor,
    child: []
  }
}

const state = {
  collapsed: true,
  currMenu: '',
  topMenuList: [],
  leftMenuList: []
}

const mutations = {
  //合成菜单数据结构
  initTopMenuList(state, allRouter) {
    state.topMenuList = allRouter
      .filter(r => r.children && r.children.length > 0) //没有child路由的，不认为是菜单项，比如login页面等
      .map(m => {
        let topMenu = routerToMenuData(m) //获取顶部菜单
        topMenu.child = m.children.map(lv1 => { //左侧1级菜单
          let lv1Menu = routerToMenuData(lv1)
          if (lv1.children && lv1.children.length > 0) { //左侧2级菜单
            lv1Menu.child = lv1.children.map(lv2 => routerToMenuData(lv2))
          }
          return lv1Menu
        })
        return topMenu
      })
    console.log('topMenuList', state.topMenuList)
  },
  //初始化左侧菜单数据
  initLeftMenuList(state, topMenuName) {
    state.leftMenuList = state.topMenuList.find(m => m.routerName === topMenuName).child
    console.log(state.leftMenuList)
  }
}

export const menu = {
  state,
  mutations
}
